package br.ufc.histogram;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;

public class TaskScheduler {
	static private HashMap<String, ArrayList<Integer>> mapping = new HashMap<String, ArrayList<Integer>>();

	public static int sum(ArrayList<Integer> arrayList){
		int sum = 0;
		for (Integer integer : arrayList) {
			sum = sum + integer;
		}
		return sum;
	}
	
	public static void taskScheduler(Histogram h){
		HistogramPosition[] histogram = h.getHistogram();
		mapping.put("10.101.10.52", new ArrayList<Integer>());
		mapping.put("localhost", new ArrayList<Integer>());

		Set<String> keys = mapping.keySet();  

		int minVm = Integer.MAX_VALUE;
		String vm = null;
		
		for (HistogramPosition hp : histogram) {

			minVm = Integer.MAX_VALUE;

			for (String key : keys)  
			{  
				if(key != null){
					int auxSum = sum(mapping.get(key));
					if (auxSum < minVm) {
						minVm = auxSum;
						vm = key;
					} 
				}
			}
			
			mapping.get(vm).add(hp.getCount());
			hp.setVm(vm);
		}
		
	}
}
